local hx__lianmeng = fk.CreateSkill {
  name = "hx__lianmeng",
  tags = { Skill.Wake },
}

hx__lianmeng:addEffect(fk.TurnEnd,{
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__lianmeng.name) and player:getMark("@hx__lianmeng_mark") >= 7
  end,
  can_wake = function(self, event, target, player, data)
    return player:getMark("@hx__lianmeng_mark") >= 7
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@hx__lianmeng_mark", 0)
    local to = room:askToChoosePlayers(player, {
      targets = room:getAlivePlayers(),
      min_num = 1,
      max_num = 1,
      prompt = "#hx__lianmeng-juexing-choose",
      skill_name = hx__lianmeng.name,
      cancelable = false,
    })
    if #to > 0 then
      local tar = to[1]
      if not tar:hasSkill("hx__zhiheng", true) then
        room:handleAddLoseSkills(tar, "hx__zhiheng", nil, true, false)
      end
      if not tar:hasSkill("hx__zhenqiao", true) then
        room:handleAddLoseSkills(tar, "hx__zhenqiao", nil, true, false)
      end
    end
    room:handleAddLoseSkills(player, "-hx__lianmeng")
  end,
})


hx__lianmeng:addEffect(fk.PindianResultConfirmed, {
  anim_type = "special",
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@hx__lianmeng_mark") < 7 and player:hasSkill(hx__lianmeng.name) and
      (data.from == player or data.to == player)
  end,
  on_use = function(self, event, target, player, data)
    player:broadcastSkillInvoke("hx__lianmeng")
    player.room:doAnimate("InvokeSkill", {
      name = "hx__lianmeng",
      player = player.id,
      skill_type = "special",
    })
    player.room:addPlayerMark(player, "@hx__lianmeng_mark", 1)
  end,
})


hx__lianmeng:addEffect(fk.EventPhaseStart, {
  anim_type = "special",
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@hx__lianmeng_mark") < 7 and player:hasSkill(hx__lianmeng.name) and 
      player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    player:broadcastSkillInvoke("hx__lianmeng")
    player.room:doAnimate("InvokeSkill", {
      name = "hx__lianmeng",
      player = player.id,
      skill_type = "special",
    })
    player.room:addPlayerMark(player, "@hx__lianmeng_mark", 1)
  end,
})


hx__lianmeng:addEffect(fk.GameStart, {
  anim_type = "special",
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@hx__lianmeng_mark") < 7 and player:hasSkill(hx__lianmeng.name)
  end,
  on_use = function(self, event, target, player, data)
    player:broadcastSkillInvoke("hx__lianmeng")
    player.room:doAnimate("InvokeSkill", {
      name = "hx__lianmeng",
      player = player.id,
      skill_type = "special",
    })
    player.room:addPlayerMark(player, "@hx__lianmeng_mark", 1)
  end,
})


hx__lianmeng:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@hx__lianmeng_mark", 0)
end)

return hx__lianmeng
